home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Constructors and exceptions
- Date: 26 Jan 1996 13:13:54 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4eak2i$hgq@dawn.mmm.com>
- References: <3107A899.4A86@tribeca.ios.com>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- John Leonard (leonardj@tribeca.ios.com) wrote:
- > Let's say that you have a class, we'll call it WindowClass, which, when it is constructed,
- > opens a file, allocates memory for the file, reads the file into memory, and then opens a window.
- > Let's say furthermore that you are going to create the object with a call to 'new' like this:
-
- > WindowClass *aWC = new WindowClass(filename);
-
- > Now let's say that during initialization of the object, after it allocates the memory it finds
- > that it cannot open the window. At this point I suppose it would be necessary to de-allocate the
- > memory that had been allocated and close the file, then throw an exception. Now as far as I can tell
- > when you use 'new' like this to allocate and initialize an object two things must happen:
- > first, space must be reserved by the 'new' function for the objects data members, then, the
- > constructor is called. If this process aborts after the memory has been allocated how can the
- > application free it?
- > Thanks,
- > John Leonard
-
- You are correct. The resources acquired by the constructor must be
- released by the constructor. If the constructor throws an exception,
- the [memory] resources acquired by new will be released for you. (If
- the compiler adheres to the standard.)
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-